class EventEmitter {

  constructor() {
    this.messageBox = {};
  }

  on(eventName, func) {
    const callbacks = this.messageBox[eventName] || [];
    callbacks.push(func);
    this.messageBox[eventName] = callbacks;
  }

  emit(eventName, ...args) {
    const callbacks = this.messageBox[eventName];
    if (callbacks.length > 0) {
      callbacks.forEach((callback) => {
        callback(...args);
      });
    }
  }

  off(eventName, func) {
    const callbacks = this.messageBox[eventName];
    const index = callbacks.indexOf(func);
    if (index !== -1) {
      callbacks.splice(index, 1);
    }
    this.messageBox[eventName] = callbacks;
  }

  once(eventName, func) {
    const fn = (...args) => {
      func.apply(this, args);
      this.off(eventName, fn);
    }
    this.on(eventName, fn);
  }
}

// 使用如下
// const event = new EventEmitter();

// const handle = (...rest) => {
//   console.log(rest);
// };

// event.on("click", handle);

// event.emit("click", 1, 2, 3, 4);

// event.off("click", handle);

// event.emit("click", 1, 2);

// event.once("dbClick", () => {
//   console.log(123456);
// });
// event.emit("dbClick");
// event.emit("dbClick");